package com.project.myxima.presenters;

import com.project.myxima.data.SubscribeDao;
import com.project.myxima.interfaces.ISubCallback;
import com.project.myxima.interfaces.ISubscribePresenter;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

public class SubscribePresenter implements ISubscribePresenter ,ISubCallback{

    SubscribeDao mSubscribeDao = SubscribeDao.getInstance();
    List<ISubCallback> mISubCallbacks = new ArrayList<>();

    //Single instance of SubscribePresenter
    private SubscribePresenter(){}
    private static class InnerHolder{
        public static final SubscribePresenter INSTANCE = new SubscribePresenter();
    }
    public static SubscribePresenter getSubscribePresenter(){
        return InnerHolder.INSTANCE;
    }

    //======================================



    @Override
    public void addSubscribe(Album album) {
        mSubscribeDao.addAlbum(album);
    }

    @Override
    public void deleteSubscribe(Album album) {
        mSubscribeDao.delAlbum(album);
    }

    @Override
    public void getSubscribeList() {

        mSubscribeDao.getAlbums();
    }

    @Override
    public void isSubscribed(Album album) {
        mSubscribeDao.isSubscribed(album);
    }

    @Override
    public void registerViewCallback(ISubCallback iSubCallback) {
        if(iSubCallback != null && !mISubCallbacks.contains(iSubCallback)){
            mISubCallbacks.add(iSubCallback);
        }

        mSubscribeDao.setCallback(this);//what it means?
    }

    @Override
    public void unregisterViewCallback(ISubCallback iSubCallback) {
        mISubCallbacks.remove(iSubCallback);
    }


    //=========================================
    //call back


    @Override
    public void onAddSubscribe(boolean isSuccess) {
        for (ISubCallback callback : mISubCallbacks){
            callback.onAddSubscribe(isSuccess);
        }
    }

    @Override
    public void onDeleteSubscribe(boolean isSuccess) {
        for (ISubCallback callback : mISubCallbacks){
            callback.onDeleteSubscribe(isSuccess);
        }
    }

    @Override
    public void onSubscribeListLoaded(List<Album> albums) {
        for (ISubCallback callback : mISubCallbacks){
            callback.onSubscribeListLoaded(albums);
        }

    }

    @Override
    public void isSubscribed(boolean isSub) {
        for (ISubCallback callback : mISubCallbacks){
            callback.isSubscribed(isSub);
        }
    }

    @Override
    public void onSubscribeListLoadedError() {
        for (ISubCallback callback : mISubCallbacks){
            callback.onSubscribeListLoadedError();
        }
    }
}
